package com.xuexi.demos.controller;
import com.xuexi.demos.entity.DTO.LoginUserDTO;
import com.xuexi.demos.entity.DTO.UserDTO;
import com.xuexi.demos.entity.VO.AuthResponse;
import com.xuexi.demos.result.Result;
import com.xuexi.demos.service.UserService;
import com.xuexi.demos.utils.SimpleJwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录注册
     */
    @PostMapping("/register")
    public Result<String> register(@RequestBody UserDTO user) {
        log.info("用户注册: {}", user);
        return Result.success(userService.Register(user));
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result<AuthResponse> login(@RequestBody LoginUserDTO userDTO) {
        // 1. 调用 service 验证用户名密码（你需要在 UserService 中实现 validateUser）
        UserDTO user = userService.login(userDTO.getUsername(), userDTO.getPassword());
        if (user == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        // 2. 生成 JWT Token
        String token = SimpleJwtUtils.generateToken(user.getUsername());
        user.setStatus(1);
        // 3. 返回 Token 和用户信息
        AuthResponse authResponse = new AuthResponse();
        authResponse.setToken(token);
        authResponse.setUser(user);


        //4，用户改为上线
        userService.updateStatus(user.getUsername(),1);
        return Result.success(authResponse);
    }

    /**
     * 添加接口获取所有用户列表
     * @return
     */
    @GetMapping("/users")
    public Result<List<String>>getAllUsers() {
        List<String> list = userService.getAllUsernames();
        return Result.success(list);
    }
    /**
     * 获取所有在线用户
     */
    @GetMapping("/online-users")
    public Result<List<String>> getOnlineUsers() {
        List<String> onlineUsers = userService.getOnlineUsersFromDB();
        return Result.success(onlineUsers);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public Result<UserDTO> getUserInfo(HttpServletRequest request) {
        // 从JWT token中获取用户名
        String username = (String) request.getAttribute("username");
        if (username == null) {
            throw new RuntimeException("用户未登录");
        }
        
        // 根据用户名获取用户信息
        UserDTO user = userService.getUserByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        
        return Result.success(user);
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request) {
        String username = (String) request.getAttribute("username");
        if (username != null) {
            // 更新用户状态为离线
            userService.updateStatus(username, 0);
        }
        return Result.success("退出成功");
    }

    /**
     * 微信登录接口
     */
    @PostMapping("/wxlogin")
    public Result<AuthResponse> wxLogin(@RequestBody String wxCode) {
        // 处理微信登录逻辑
        // 这里需要根据你的微信登录业务逻辑来实现
        // 暂时返回一个示例
        throw new RuntimeException("微信登录功能待实现");
    }

}